
<template>
  <div>
    <p-modal
      title="Title"
      width="80%"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <!-- <template slot="footer">
        <p-button key="back" @click="handleCancel">
          取消
        </p-button>
        <p-button type="primary" @click="save">保存</p-button>
      </template> -->

      <p-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <p-row :gutter="24">
          <p-col :xl="8" :lg="12" :md="12" :sm="24">
            <p-form-model-item ref="orderno" label="单据号" prop="orderno">
              <p-input v-model="form.orderno" />
            </p-form-model-item>
          </p-col>
          <p-col :xl="8" :lg="12" :md="12" :sm="24">
            <p-form-model-item ref="skillNo" label="制单人" prop="skillNo">
              <p-input disabled :value="username" />
            </p-form-model-item>
          </p-col>
          <p-col :xl="8" :lg="12" :md="12" :sm="24">
            <p-form-model-item ref="makeDate" label="制单日期" prop="makeDate">
              <p-input disabled :value="form.makeDate" />
            </p-form-model-item>
          </p-col>
          <p-col :xl="8" :lg="12" :md="12" :sm="24">
            <p-form-model-item
              ref="workroomname"
              label="车间"
              prop="workroomname"
            >
              <p-input disabled v-model="form.workroomname" />
            </p-form-model-item>
          </p-col>
          <p-col :xl="8" :lg="12" :md="12" :sm="24">
            <p-form-model-item ref="classname" label="班组" prop="classname">
              <p-input disabled v-model="form.classname" />
            </p-form-model-item>
          </p-col>
          <p-col :xl="8" :lg="12" :md="12" :sm="24">
            <p-form-model-item ref="lineid" label="线体" prop="lineid">
              <p-select v-model="form.lineid" @change="linenameChange">
                <p-select-option
                  :value="item.lineid + ''"
                  v-for="(item, index) in classLineInfoList"
                  :key="index"
                >
                  {{ item.linename }}
                </p-select-option>
              </p-select>
            </p-form-model-item>
          </p-col>
        </p-row>
      </p-form-model>
    </p-modal>
  </div>
</template>
<script>
import { getDictItemsArr } from "@/utils/util";
import { postAction, getAction, deleteAction, putAction } from "@/api/manage";
import moment from "moment";

export default {

  data() {
    return {
      state: "add",
      visible: false,
      confirmLoading: false,
      labelCol: { span: 12 },
      wrapperCol: { span: 12 },
      other: "",
      editId: "",
      form: {
        orderno: "",
        makeDate: "",
        workroomname: "",
        workroomid: "",
        workroomno: "",
        classno: "",
        classid: "",
        classname: "",
        lineid: "",
        linename: "",
        lineno: "",
      },
      rules: {
        orderno: [{ required: true, message: "请输入单据号", trigger: "blur" }],
        lineid: [{ required: true, message: "请选择线体", trigger: "blur" }],
      },
      // 班组信息列表
      classLineInfoList: [],
      url: {
        add: "/mes-manpower/mesHrMemberSkillAdjust",
        edit: "/mes-manpower/mesHrMemberSkillAdjust",
        classLineInfoList: "/mes-manpower/mesHrClassLineInfo/classLineInfoList",
      },
    };
  },

  computed: {
    username() {
      return this.$store.state.user.name;
    },
  },

  mounted() {
    this.getClassLineInfoList();
  },
  methods: {
    moment,

    // 获取班组信息列表
    getClassLineInfoList() {
      getAction(this.url.classLineInfoList).then((res) => {
        if (res.success) {
          this.classLineInfoList = [...res.data];
        }
      });
    },

    // 选择线体
    linenameChange(lineid) {
      console.log(lineid);
      this.classLineInfoList.forEach((ele) => {
        if ((ele.lineid = lineid)) {
          [
            "workroomname",
            "workroomid",
            "workroomno",
            "classno",
            "classid",
            "classname",
            "lineid",
            "linename",
            "lineno",
          ].forEach((key) => {
            this.form[key] = ele[key];
          });
          this.form.classid = ele.id;
        }
      });
    },

    // 获取技能列表
    getSkillList() {
      getAction(this.url.skillList).then((res) => {
        if (res.success) {
          this.skillList = res.data;
        }
      });
    },

    // 新增考核详情
    addAssessDetail(data) {
      this.form.detailDtos.push(JSON.parse(JSON.stringify(data)));
    },

    handleDelete(index) {
      this.form.detailDtos.splice(index, 1);
    },

    showModal(record) {
      Object.assign(this.$data.form, this.$options.data().form);
      this.form.makeDate = this.moment().format("YYYY-MM-DD");
      if (record) {
        this.state = "edit";
        console.log(record);
        for (let key in this.form) {
          this.form[key] = record[key];
        }

        this.editId = record.id;
      } else {
        this.state = "add";
      }
      this.visible = true;
    },

    handleOk(e) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.confirmLoading = true;
          const params = JSON.parse(JSON.stringify(this.form));
          if (this.state == "add") {
            postAction(this.url.add, params).then((res) => {
              if (res.success) {
                this.visible = false;
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }

          if (this.state == "edit") {
            params.id = this.editId;
            putAction(this.url.edit, params).then((res) => {
              if (res.success) {
                this.visible = false;
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }
        }
      });
    },

    handleCancel(e) {
      console.log("Clicked cancel button");
      this.visible = false;
    },
  },
  components: {},
};
</script>
